angular.module('versionGroupREditApp', []).controller( 'versionGroupREditCtr',
	function($scope, $http) {
	// 模型名称校验默认不显示
	$scope.checkUserAccount = false;
	$scope.id = angular.element($("#id")).val();  //获取id
 
	$http({  
		url:basePath+'/user/queryVersionGroupRList',  
		params: {  
			'id':$scope.id  
	    },  
		method:'POST'  
		}).success(function(data,header,config,status){ 
			$scope.groupName = data[0].groupName;  
			$scope.groupId = data[0].groupId; 
		}).error(function(data,header,config,status){  
			  toastr.warning("操作失败！");  
  		});
			
		// 点击保存按钮，新增一条数据
		$scope.editVersionGroupR = function() {
			// form表单的验证 数据不对，return false
			var formValid = true;
			if (!$("#versionId").val()) {
				toastr.warning("版本不可为空！");
				formValid = false;
				return false;
			}
			if (formValid) { // 表单通过验证后提交
				checkUserAccount(); // 校验属性名称是否重复
			}
		};

		function checkUserAccount() {
			var result = true;
			$http({
				url : basePath+'/user/queryVersionGroupRList',
				method : 'POST',
				params : {
					'groupId' : $scope.groupId,
					'versionId' : $("#versionId").val()
				}
			}).success(function(data, header, config, status) {
				if(data.length==0){
					submitData();
				}else{
					toastr.warning("该配置已存在");
					return;
				}
			}).error(function(data, header, config, status) {
				// 处理响应失败
				result = false;
			});
		}
		// form表单提交方法 submit(若是校验都通过啦，数据便可以提交啦)
		function submitData() {
		 
			var form = $("form[name=versionGroupRFormEdit");  
			var options  = {
				url:basePath+'/user/updateVersionGroupR',
                type:'post',  
                data:{
                	'groupId' : $scope.groupId,
					'versionId' : $("#versionId").val()
                },  
                success:function(data){  
                   if(data){ 
                           toastr.success('操作数据成功!'); 
                           setTimeout(function() { 
                        	   location.href = basePath+'/user/versionGroupR_apply';
                           }, 500); 
                     }else{ 
                          toastr.error('操作数据失败!'); 
                          setTimeout(function() {
                        	  location.href = basePath+'/user/versionGroupR_apply';
          				}, 500);
                     } 

            	}  
        };  
        form.ajaxSubmit(options);  
	}
		
		//  点击取消按钮，跳转到查询页面
		$scope.backHis = function() {
			location.href = basePath+'/user/versionGroupR_apply';
		};
});
$(window).on('load', function () {  
	  
    $('.selectpicker').selectpicker({  
        'selectedText': 'cat'  
    });  
  
});